package com.example.winner.handlermsgprogress;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.os.Message;

public class MainActivity extends AppCompatActivity {
    private Button startbt=null;
    private ProgressBar bar=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startbt=(Button)findViewById(R.id.start);
        bar=(ProgressBar)findViewById(R.id.bar);
        startbt.setOnClickListener(new ButtonListener());
    }
    Handler handler=new Handler();
    class ButtonListener implements View.OnClickListener
    {
        @Override
        public void onClick(View view)
        {
            bar.setVisibility(View.VISIBLE);
            handler.post(updateThread);
        }
    }
    Runnable updateThread=new Runnable() {
        int i=0;
        @Override
        public void run() {
            System.out.println("Begin Thread");
            i=i+10;
            Message msg=handler.obtainMessage();
            msg.arg1=i;
            try
            {
                Thread.sleep(1000);
            }catch(InterruptedException e)
            {
                e.printStackTrace();
            }
            handler.sendMessage(msg);
            if(i==100)
            {
                handler.removeCallbacks(updateThread);
            }
        }
    };
}
